package com.ytf.test.fifth;


/**
 * @author: YTF
 * @date: 2024/9/19 9:19
 * @version: 1.0
 * @description: 终结条件 finalize 提供一个清理的方法 @TODO 没理解 这个还有gc
 */
public class TerminationCondition {
    public static void main(String[] args) {
        Book novel = new Book(true,"book1");
        // proper cleanup
        novel.checkIn();
        novel=null;
        // Drop the reference, forget to clean up
        new Book(true,"book2");
        // Force garbage collection & finalization
        System.gc();
    }
}
class Book{
    Boolean checkedOut=false;
    String name="";
    public Book() {
    }

    public Book(Boolean checkedOut) {
        this.checkedOut = checkedOut;
    }
    void checkIn(){
        checkedOut=false;
    }

    public Book(Boolean checkedOut, String name) {
        this.checkedOut = checkedOut;
        this.name = name;
    }

    @Override
    protected void finalize() throws Throwable {
        System.out.println("Error: checked out name:"+name+" checkedOut: "+checkedOut);
        // if (checkedOut){
        //     System.out.println("Error: checked out name:"+name);
            // Normally, you'll also do this;
            // super.finalize(); // Call the base-class version
        // }
    }
}
